package com.sias.regexp;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-03 13:21:38
 */
public class MatcherMethod {
  public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
    String content = "hello edu jack hspedutom hello smith hello hspedu hspedu";
    String regStr = "hello.*";
    Pattern pattern = Pattern.compile(regStr);
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()){
      System.out.println("=============");
      System.out.println(matcher.start());
      System.out.println(matcher.end());
      System.out.println("找到："+content.substring(matcher.start(),matcher.end()));
    }
    System.out.println("整体匹配："+matcher.matches());


    regStr = "hspedu";
    pattern = Pattern.compile(regStr);
    matcher = pattern.matcher(content);
    String newContent = matcher.replaceAll("吴文杰");
    System.out.println("newContent="+newContent);



    // Class<Matcher> cls = Matcher.class;
    // Constructor<Matcher> constructor = cls.getDeclaredConstructor();
    // constructor.setAccessible(true);
    // Matcher matcher1 = constructor.newInstance();
    // Field last = cls.getDeclaredField("last");
    // last.setAccessible(true);
    // Object o = last.get(matcher1);
    // System.out.println(o);
  }
}
